package lingnan.cn.edu.dialog;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import lingnan.cn.edu.BaseAndImpl.StaffManageService;
import lingnan.cn.edu.User.Staffinf;
import lingnan.cn.edu.service.StaffManageServiceImpl;
import lingnan.cn.edu.view.ManageView;

public class StaffManageDialog extends JDialog implements ActionListener {
	private JPanel namePanel,passwordPanel,superPanel,opePanel;
	
	private JLabel nameLabel,passwordLabel,superLabel;
	private JTextField nameTF,passwordTF,superTF;
	
	private JButton saveBtn,cancelBtn;
	
	private StaffManageService staffmanageService = new StaffManageServiceImpl();
	
	private ManageView manageView;
	
	private Staffinf staffinf;
	
	private String title = "null";
	private String staffName = "null";
	/*public StaffManageDialog(JFrame parent,ManageView manageView) {
		super(parent,"添加");
		this.manageView = manageView;
		
		setSize(350,300);
		
		setLocationRelativeTo(null);
		
		setModal(true);
		setResizable(false);
		 
		this.setLayout(new FlowLayout());
		
		initView();
	}*/
	
	public StaffManageDialog(JFrame parent,ManageView manageView,String title) {
		super(parent,title);
		this.title = title;
		this.manageView = manageView;
		
		setSize(350,300);
		
		setLocationRelativeTo(null);
		
		setModal(true);
		setResizable(false);
		 
		this.setLayout(new FlowLayout());
		
		initView();
	}
	
	public StaffManageDialog(JFrame parent,ManageView manageView,String title,Staffinf staffinf) {
		super(parent,title);
		this.title = title;
		this.manageView = manageView;
		
		setSize(350,300);
		
		setLocationRelativeTo(null);
		
		setModal(true);
		setResizable(false);
		 
		this.setLayout(new FlowLayout());
		
		updateView(staffinf);
	}
	private void initView() {
		namePanel = new JPanel();
		nameLabel = new JLabel("姓名");
		nameTF = new JTextField(15);
		namePanel.add(nameLabel);
		namePanel.add(nameTF);
		
		passwordPanel = new JPanel();
		passwordLabel = new JLabel("密码");
		passwordTF = new JTextField(15);
		passwordPanel.add(passwordLabel);
		passwordPanel.add(passwordTF);
		
		superPanel = new JPanel();
		superLabel = new JLabel("权限");
		superTF = new JTextField(15);
		superPanel.add(superLabel);
		superPanel.add(superTF);
				
		
		opePanel = new JPanel();
		saveBtn = new JButton("保存");
		cancelBtn = new JButton("取消");
		saveBtn.addActionListener(this);
		cancelBtn.addActionListener(this);
		opePanel.add(saveBtn);
		opePanel.add(cancelBtn);
		
		Container container = getContentPane();
		container.add(namePanel);
		container.add(passwordPanel);
		container.add(superPanel);
		container.add(opePanel);
	}

	private void updateView(Staffinf staffinf) {
		namePanel = new JPanel();
		nameLabel = new JLabel("姓名");
		nameTF = new JTextField(15);
		nameTF.setText(staffinf.getStaffName());
		staffName = staffinf.getStaffName();
		namePanel.add(nameLabel);
		namePanel.add(nameTF);
		
		passwordPanel = new JPanel();
		passwordLabel = new JLabel("密码");
		passwordTF = new JTextField(15);
		passwordTF.setText(staffinf.getPassword());
		passwordPanel.add(passwordLabel);
		passwordPanel.add(passwordTF);
		
		superPanel = new JPanel();
		superLabel = new JLabel("权限");
		superTF = new JTextField(15);
		superTF.setText(String.valueOf(staffinf.getStaffSuper()));
		superPanel.add(superLabel);
		superPanel.add(superTF);
		
		
		opePanel = new JPanel();
		saveBtn = new JButton("更新");
		cancelBtn = new JButton("取消");
		saveBtn.addActionListener(this);
		cancelBtn.addActionListener(this);
		opePanel.add(saveBtn);
		opePanel.add(cancelBtn);
		
		Container container = getContentPane();
		container.add(namePanel);
		container.add(passwordPanel);
		container.add(superPanel);
		container.add(opePanel);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		if (source == saveBtn) {
			String name = nameTF.getText();
			String staffsuper = superTF.getText();
			String password = passwordTF.getText();
			Staffinf staffinf = new Staffinf();
			staffinf.setStaffName(name);
			staffinf.setPassword(password);
			staffinf.setStaffSuper(Integer.parseInt(staffsuper));
			if(title.equals("添加")) {				
				int result = staffmanageService.addStaff(staffinf);
				if(result==1) {
					JOptionPane.showMessageDialog(this,"添加成功","提示",JOptionPane.INFORMATION_MESSAGE);
					this.dispose();
					//刷新
					manageView.staffrefresh();
				}else {
					JOptionPane.showMessageDialog(this,"添加失败","提示",JOptionPane.ERROR_MESSAGE);
				}
			}else {
				int updateresult = staffmanageService.updateStaff(staffinf, staffName);
				if(updateresult==1) {
					JOptionPane.showMessageDialog(this,"更新成功","提示",JOptionPane.INFORMATION_MESSAGE);
					this.dispose();
					//刷新
					manageView.staffrefresh();
				}else {
					JOptionPane.showMessageDialog(this,"更新失败","提示",JOptionPane.ERROR_MESSAGE);
				}
			}
			
		}else if(source == cancelBtn) {
			
			this.dispose();
		}
	}
}

